<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>作业</title>
    <script>
        //1.数组反转  输入 [1,2,3,4,5]01234  得到  [5,4,3,2,1]
        function reverse(arr) {
            for (let i = 0; i < arr.length / 2; i++) {
                let n = arr.length - 1;
                if (arr.length % 2 === 0) {
                    let temp = arr[i];
                    arr[i] = arr[n - i];
                    arr[n - i] = temp;
                } else {
                    if (i === (arr.length - 1) / 2) {
                        continue;
                    } else {
                        let temp = arr[i];
                        arr[i] = arr[n - i];
                        arr[n - i] = temp;
                    }
                }
            }
        }
        let arr1 = [1, 2, 3, 4, 5];
        reverse(arr1);
        console.log(arr1);
        //2.取交集  输入 [1,2,3]  [3,4,5]  得到 [3]
        function mutual(arr1, arr2) {
            let arr = [], n = 0;
            for (let i = 0; i < arr1.length; i++) {
                for (let j = 0; j < arr2.length; j++) {
                    if (arr1[i] === arr2[j]) {
                        arr[n++] = arr1[i];
                    }
                }
            }
            return arr;
        }
        let arr2 = [1, 2, 3], arr3 = [3, 4, 5];
        console.log(mutual(arr2, arr3));
        //3.取差集  输入 [1,2,3]  [3,4,5]  得到 [1,2,4,5]
        function diff(arr1, arr2) {
            let arr = [], length,n=0;
            let difNum = mutual(arr1, arr2);
            if (arr1.length > arr2.length) {
                length = arr1.length;
            } else {
                length = arr2.length;
            }
            for (let i = 0; i < length; i++) {
                let count1 = 0, count2 = 0;
                for (let j = 0; j < difNum.length; j++) {
                    if (arr1[i] !== difNum[j]) {
                        count1++;
                    }
                    if (arr2[i] !== difNum[j]) {
                        count2++;
                    }
                }
                if (count1 === difNum.length) {
                    arr[n++] = arr1[i]
                }
                if (count2 === difNum.length) {
                    arr[n++] = arr2[i]
                }
            }
            return arr;
        }
        console.log(diff(arr2, arr3));
    </script>
</head>

<body>

</body>

</html>